<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Log;
use think\Request;

class Index extends Common
{
    public function index()
    {
        if (Request()->isPost()) {
            $input = Request()->param();

            $id = $input["id"];
            $name = $input['name'];
            $mobile = $input['mobile'];
            $full_price_send = $input['full_price_send'];
            $business_range_time = $input['business_range_time'];
            $booking_range_time = $input['booking_range_time'];
            //            $img = $input['img'];
            $lat = $input['lat'];
            $lng = $input['lng'];
            $address = $input['address'];

            $time = time();

            if (empty($name)) adminReturn(2, '请输入门店名称');
            if (empty($mobile)) adminReturn(2, '请输入门店电话');

            if ($id) $where['id'] = ['neq', $id];
            $where['name'] = $name;
            $count = Db::name('shop')->where($where)->count();
            if ($count) adminReturn(2, '门店名称已存在');

            $data = [
                'name' => $name,
                'mobile' => $mobile,
                'full_price_send' => $full_price_send,
                'business_range_time' => $business_range_time,
                'booking_range_time' => $booking_range_time,
                //                'img' => $img,
                'lat' => $lat,
                'lng' => $lng,
                'address' => $address
            ];

            if ($id > 0) {
                $data['update_time'] = $time;
                $res = Db::name('shop')->where(['id' => $id])->update($data);
            } else {
                $data['create_time'] = $time;
                $res = Db::name('shop')->insertGetId($data);
            }

            if ($res) {
                adminReturn(1, '保存成功');
            } else {
                adminReturn(2, '保存失败');
            }
        } else {
            $id = session('shop_id');
            $this->assign('shop_id', $id);

            if ($id) {
                $info = Db::name('shop')->where(['id' => $id])->find();
                if ($info['lat'] && $info['lng']) {
                    $map_url = 'https://apis.map.qq.com/tools/locpicker?type=1&search=1&policy=1&key=D4GBZ-7XF64-DQ5UN-KR24K-SY5YF-CZFSA&referer=myapp&coord=' . $info['lat'] . ',' . $info['lng'];
                } else {
                    $map_url = 'https://apis.map.qq.com/tools/locpicker?type=1&search=1&policy=1&key=D4GBZ-7XF64-DQ5UN-KR24K-SY5YF-CZFSA&referer=myapp';
                }
            } else {
                $map_url = 'https://apis.map.qq.com/tools/locpicker?type=1&search=1&policy=1&key=D4GBZ-7XF64-DQ5UN-KR24K-SY5YF-CZFSA&referer=myapp';
            }

            $this->assign('info', $info);
            $this->assign('map_url', $map_url);
            $this->assign('title', '门店信息');

            $user_list = Db::name('user')->where(['is_admin' => 0, 'status' => 1])->select();
            if ($user_list) {
                foreach ($user_list as &$v) {
                    $v['checked'] = in_array($v['id'], explode(',', $info['user_ids'])) ? 'checked' : '';
                }
            }
            $this->assign('user_list', $user_list);

            return $this->view->fetch();
        }
    }


}
